﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MacNs.Properties;
using Streambolics.Gui;
using Streambolics.App;
using Streambolics;

namespace MacNs
{
    public partial class MacNsMainForm : AppForm
    {
        private NameDatabase _Database = new NameDatabase ();
        private MacToNameConverter _Converter = new MacToNameConverter ();
        private MacFilter _Filter = new MacFilter ();
        private IpToMacScanner _Scanner;
        private HostsFileWriter _Writer;

        public MacNsMainForm ()
        {
            InitializeComponent ();

            _Scanner = new IpToMacScanner (_Database, _Converter, _Filter);
            _Writer = new HostsFileWriter (_Database);

            _Database.Changed += new NameDatabaseEventHandler (_Database_Changed);
            Settings.Default.SettingsLoaded += new System.Configuration.SettingsLoadedEventHandler (Default_SettingsLoaded);
            Settings.Default.PropertyChanged += new PropertyChangedEventHandler (Default_PropertyChanged);
            RebuildFriendlyNames ();
        }

        void Default_PropertyChanged (object sender, PropertyChangedEventArgs e)
        {
            RebuildFriendlyNames ();
        }

        private void RebuildFriendlyNames ()
        {
            _Converter.Rebuild (Settings.Default.FriendlyNames);
            _Filter.FilterText = Settings.Default.Filter;
            _Writer.HostsFile = Settings.Default.HostsFile;
            _Scanner.Subnet = Settings.Default.Subnet;

            _Scanner.Start ();
            _Writer.Start ();
        }

        void Default_SettingsLoaded (object sender, System.Configuration.SettingsLoadedEventArgs e)
        {
            RebuildFriendlyNames ();
        }

        void _Database_Changed (object sender, NameDatabaseEventArgs e)
        {
            edit_LmHosts.Text = _Database.AsHostsFile ();
        }

        protected override void OnLoad (EventArgs e)
        {
            base.OnLoad (e);
            GlobalLog.EventLogged += new GlobalLogEventHandler (GlobalLog_EventLogged);
        }

        void GlobalLog_EventLogged (object sender, GlobalLogEventArgs e)
        {
            toolStrip_Status.Text = e.Message;
        }

        private void button_IpToMac_Click (object sender, EventArgs e)
        {
            try
            {
                edit_Output.Text = ArpClient.IpToMacName (edit_Ip.Text);
            }
            catch (Exception ex)
            {
                edit_Output.Text = ex.Message;
            }
        }

        private void button_MacToIp_Click (object sender, EventArgs e)
        {
            try
            {
                edit_Output.Text = _Database.Resolve (edit_Mac.Text);
            }
            catch (Exception ex)
            {
                edit_Output.Text = ex.Message;
            }
        }

        protected override bool IsServer ()
        {
            return true;
        }
    }
}
